//
//  CBVedioDetailViewController.m
//  CultureBreath
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "CBVedioDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "CBVedioModel.h"
#import "CBVedioTopView.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UMSocial.h"
@interface CBVedioDetailViewController () <UMSocialUIDelegate>

@property (nonatomic, strong) MPMoviePlayerViewController *VedioPlayer;

@end

@implementation CBVedioDetailViewController

- (void)getBackCBVedioListViewController:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CBVedioTopView *topView = [[CBVedioTopView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 50)];
    [topView.button addTarget:self action:@selector(getBackCBVedioListViewController:) forControlEvents:UIControlEventTouchUpInside];
    [topView.eyeButton setImage:[UIImage imageNamed:@"eyeM"] forState:UIControlStateNormal];
    [topView.eyeButton addTarget:self action:@selector(getBackCBVedioListViewController:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:topView];
    
    [self ShufflViewOfPicture];
    
    // Do any additional setup after loading the view.
}

- (void)ShufflViewOfPicture
{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT)];
    scrollView.contentSize = CGSizeMake(self.dataArray.count * SCREEN_WIDTH, 0);
    scrollView.contentOffset = CGPointMake(SCREEN_WIDTH*self.index, 0);
    scrollView.pagingEnabled = YES;
    for (int i = 0; i < self.dataArray.count; i++) {
        _vedioDetailView = [[CBVedioDetailView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
        CBVedioModel *model = self.dataArray[i];
        [_vedioDetailView.screenPicture sd_setImageWithURL:[NSURL URLWithString:model.coverBlurred]];
        
        [_vedioDetailView.pictureView sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
        
        _vedioDetailView.playButton.tag = 100 + i;
        [_vedioDetailView.playButton addTarget:self action:@selector(vedioPlay:) forControlEvents:UIControlEventTouchUpInside];
        
        _vedioDetailView.titleLabel.text = model.title;
        _vedioDetailView.typeTimeLabel.text = [NSString stringWithFormat:@"#%@ / %0.2ld'%0.2ld",model.category, model.duration / 60, model.duration % 60];
        _vedioDetailView.descLabel.text = model.desc;
        
        [_vedioDetailView.likeButton addTarget:self action:@selector(collectionTheActivity:) forControlEvents:UIControlEventTouchUpInside];
        _vedioDetailView.likeNumLabel.text = [NSString stringWithFormat:@"%@",model.consumptionModel.collectionCount];
        _vedioDetailView.shareNumLabel.text = [NSString stringWithFormat:@"%@",model.consumptionModel.shareCount];
        
        _vedioDetailView.shareButton.tag = 100 + i;
        [_vedioDetailView.shareButton addTarget:self action:@selector(shareTheInfomationForPerson:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:_vedioDetailView];
    }
    [self.view addSubview:scrollView];
}

- (void)vedioPlay:(UIButton *)button
{
    CBVedioModel *model = self.dataArray[button.tag - 100];
    _VedioPlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:model.playUrl]];
    [_VedioPlayer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentMoviePlayerViewControllerAnimated:_VedioPlayer];
//    self.VedioPlayer.moviePlayer.repeatMode = MPMovieRepeatModeNone;
//    [self.VedioPlayer.view setFrame:[UIScreen mainScreen].bounds];
//    [self.view addSubview:self.VedioPlayer.view];
//    [self.VedioPlayer.moviePlayer play];
}

// 支持横竖屏
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAll;
}

- (void)shareTheInfomationForPerson:(UIButton *)button
{
    CBVedioModel *model = self.dataArray[button.tag - 100];
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"561c5f76e0f55ab642001b90" shareText:model.webUrl shareImage:[UIImage imageNamed:model.coverForDetail] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren, nil] delegate:self];
}

- (void)collectionTheActivity:(UIButton *)button
{
    button.selected = !button.selected;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
